//
//  HMComposeTextView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/15.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

class HMComposeTextView: UITextView {

    var placeholder: String? {
        
        didSet {
            
            placeHolder.text = placeholder
        }
    }
    
    override var font: UIFont? {
        
        didSet {
            
            placeHolder.font = font
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer:textContainer )
        
        setupUI()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        addSubview(placeHolder)
        
        placeHolder.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(5)
            make.leading.equalTo(self).offset(8)
            make.width.equalTo(SCREENW - 10)
        }
        // 通知监听
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HMComposeTextView.textChange), name: UITextViewTextDidChangeNotification, object: nil)

    }
    
    // 监听方法
    @objc private func textChange() {
        
        placeHolder.hidden = hasText()
        
    }
    
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: -懒加载
    // 占位符
    private lazy var placeHolder: UILabel = {
       
       let label = UILabel(title: "奥运会期间马蓉获得了一块儿淫牌，奥运会期间马蓉获得了一块儿淫牌，奥运会期间马蓉获得了一块儿淫牌，奥运会期间马蓉获得了一块儿淫牌", color: UIColor.lightGrayColor())
       label.textAlignment = .Left
       label.font = UIFont.systemFontOfSize(14)
       label.numberOfLines = 0
       label.sizeToFit()
       return label
        
    }()
    

}
